<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" allow-clear class="w-64" placeholder="项目编号/项目名称/助记码" />
        </a-form-item>
        <a-form-item name="isYX" class="w-64 ml-6">
          <!-- 查看传1 不查看传0 -->
          <a-checkbox v-model:checked="formState.isYX" @change="getData()">未停用项目</a-checkbox>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable :loading="loading" column-code="000470" fill :data-source="tableData" bordered row-key="bh" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'yxzt'">
            <a-tag v-if="record.yxzt" color="red">停用</a-tag>
            <a-tag v-else color="blue">启用</a-tag>
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg }" />
          </template>
          <template v-if="column.dataIndex === 'prefix'">
            <EditCell title="手术病案号前缀">
              <a-input v-model:value="record.prefix" placeholder="请输入" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'outorder'">
            <EditCell title="级别">
              <a-select v-model:value="record.outorder" @select="onBlurInput(record)">
                <a-select-option v-for="item in 9" :key="item" :value="item">{{ item }}</a-select-option>
              </a-select>
            </EditCell>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const formRef = ref(null)
const formState = ref({})
const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getSurgicalItemsList',
  immediate: true,
  params: formState.value,
  formatParams: () => {
    return {
      zjm: formState.value.zjm,
      isYX: formState.value.isYX ? '0' : '1'
    }
  }
})
const onBlurInput = (record) => {
  saveData(record)
}
const saveData = async (record) => {
  delete record.createTime
  delete record.uploadTime
  const { code, msg } = await post('/operation/opex/doSaveOphthNumberRuleBjmsg', { ...record, icd9: record.bh })
  if (code !== 0) return message.error(msg)
  message.success(msg || '操作成功')
  getData()
}
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
